package com.lx.crm.workbench.web.controller;

import com.lx.crm.commons.contants.Contants;
import com.lx.crm.commons.domain.ReturnObject;
import com.lx.crm.commons.utils.DateUtils;
import com.lx.crm.commons.utils.UUIDUtils;
import com.lx.crm.settings.domain.User;
import com.lx.crm.workbench.domain.ActivityRemark;
import com.lx.crm.workbench.service.ActivityRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 雒鑫
 */
@Controller
public class ActivityRemarkController {
    @Autowired
    private ActivityRemarkService activityRemarkService;

    /**
     * 保存创建的市场活动备注信息
     *
     * @param remark
     * @param session
     * @return
     */
    @RequestMapping("workbench/activity/saveCreateActivityRemark.do")
    @ResponseBody
    public Object saveCreateActivityRemark(ActivityRemark remark, HttpSession session) {
        User user = (User) session.getAttribute(Contants.SESSION_USER);
        //二次封装参数
        remark.setId(UUIDUtils.getUUID());
        remark.setCreateTime(DateUtils.formateDateTime(new Date()));
        remark.setCreateBy(user.getId());
        remark.setEditFlag(Contants.REMARK_EDIT_NO_EDITED);
        ReturnObject returnObject = new ReturnObject();
        //调用service方法保存创建的市场活动
        try {
            int ret = activityRemarkService.saveCreateActivityRemark(remark);
            if (ret > 0) {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setRetData(remark);
            } else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙...请稍后重试...");
            }

        } catch (Exception e) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            throw new RuntimeException(e);
        }
        return returnObject;
    }

    /**
     * 根据id删除市场活动备注信息
     *
     * @param id
     * @return
     */
    @RequestMapping("workbench/activity/deleteActivityRemarkById.do")
    @ResponseBody
    public Object deleteActivityRemarkById(String id) {
        ReturnObject returnObject = new ReturnObject();
        try {
            //调用service方法删除备注
            int ret = activityRemarkService.deleteActivityRemarkById(id);
            if (ret > 0) {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            } else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙..请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙..请稍后重试");
        }
        return returnObject;

    }

    /**
     * 保存修改的市场活动备注
     * @param activityRemark
     * @param session
     * @return
     */
    @RequestMapping("workbench/activity/savaEditActivityRemark.do")
    @ResponseBody
    public Object savaEditActivityRemark(ActivityRemark activityRemark, HttpSession session) {
        User user = (User) session.getAttribute(Contants.SESSION_USER);
        //二次封装
        activityRemark.setEditTime(DateUtils.formateDateTime(new Date()));
        activityRemark.setEditBy(user.getId());
        activityRemark.setEditFlag(Contants.REMARK_EDIT_YES_EDITED);

        ReturnObject returnObject = new ReturnObject();
        try {
            //调用service方法保存修改的市场活动备注
            int ret = activityRemarkService.saveEditActivityRemark(activityRemark);

            if (ret > 0) {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setRetData(activityRemark);
            } else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙...请稍后");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙...请稍后");
        }
        return returnObject;
    }
}
